let format = new Object()
format.formatCityName = arr => {
  let newArr = []
  arr.forEach((item, index) => {
    let obj = {}
    for (let k in item) {
      if (k == "id") {
        obj["value"] = item["id"]
      }
      if (k == "name") {
        obj["label"] = item["name"]
      }
      obj[k] = item[k]
      obj["loading"] = false
      obj.children = []
    }
    newArr.push(obj)
  })
  return newArr
}

format.formatCompanyName = arr => {
  let newArr = []
  arr.forEach((item, index) => {
    let obj = {}
    for (let k in item) {
      if (k == "id") {
        obj["value"] = item["id"]
      }
      if (k == "name") {
        obj["label"] = item["name"]
      }
      obj[k] = item[k]
      // obj['loading'] = false; obj.children = [];
    }
    newArr.push(obj)
  })
  return newArr
}

format.formatDeviceName = arr => {
  let newArr = []
  arr.forEach((item, index) => {
    let obj = {}
    for (let k in item) {
      if (k == "id") {
        obj["value"] = item["id"]
      }
      if (k == "name") {
        obj["label"] = item["name"]
      }
      obj[k] = item[k]
      // obj['loading'] = false; obj.children = [];
    }
    newArr.push(obj)
  })
  return newArr
}

format.formatDeviceDetail = (arr, otherArr) => {
  arr.map(t => {
    // todo 1拖2需要加上device地址判断
    var param = otherArr.filter(f => {
      return f.paramsType == t.type
    })

    var p = {}
    if (param.length == 1) {
      p = param[0]
    }
    if (p) {
      if (
        ((t.type >= 5 && t.type <= 8) || (t.type >= 12 && t.type <= 15)) &&
        parseFloat(t.value) > p.warnUp
      ) {
        t.state = 2
      } else if (t.type >= 20 && t.type <= 23) {
        // 功率因数
        if (parseFloat(t.value) < 0.9) {
          t.state = 2
        } else {
          t.state = 1
        }
      } else if (t.type == 30) {
        // 频率
        if (parseFloat(t.value) < 49.5 || parseFloat(t.value) > 50.5) {
          t.state = 2
        } else {
          t.state = 1
        }
      } else if (t.type == 24) {
        // 电压不平衡度
        if (parseFloat(t.value) > 5) {
          t.state = 2
        } else {
          t.state = 1
        }
      } else if (t.type == 25) {
        // 电流不平衡度
        if (parseFloat(t.value) > 30) {
          t.state = 2
        } else {
          t.state = 1
        }
      } else if (t.type >= 38 && t.type <= 52) {
        // 电压谐波含量
        if (parseFloat(t.value) > 5) {
          t.state = 2
        } else {
          t.state = 1
        }
      } else if (t.type >= 53 && t.type <= 55) {
        // 电压合格率
        if (parseFloat(t.value) > 0 && parseFloat(t.value) < 95) {
          t.state = 2
        } else {
          t.state = 1
        }
      } else if (t.type >= 56 && t.type <= 58) {
        // 电压波形失真
        if (parseFloat(t.value) > 4) {
          t.state = 2
        } else {
          t.state = 1
        }
      } else if (t.type >= 59 && t.type <= 61) {
        // 电流波形失真
        if (parseFloat(t.value) > 15) {
          t.state = 2
        } else {
          t.state = 1
        }
      } else {
        t.state = 1
      }
    }
  })
}

const transArrToLabel = (arr = []) => {
  if (arr.length == 0) {
    return
  }
  for (let i = 0; i < arr.length; i++) {
    for (let key in arr[i]) {
      arr[i]["label"] = arr[i]["name"]
      arr[i][key] = arr[i][key]
    }
  }
  // console.log( [].concat(arr))
  return [].concat(arr)
}
format.formatTree = (arr, obj) => {
  let newArr = transArrToLabel(arr)
  for (let i = 0; i < newArr.length; i++) {
    newArr[i].children = transArrById(newArr[i].id, obj)
  }
  return newArr
}

const transArrById = (id, obj) => {
  let arr = obj[id]
  if (JSON.stringify(obj) == "{}") {
    return
  }
  return transArrToLabel(arr)
}

const transArrToLabel1 = (arr = []) => {
  if (arr.length == 0) {
    return
  }
  for (let i = 0; i < arr.length; i++) {
    for (let key in arr[i]) {
      arr[i]["label"] = arr[i]["alias"]
      arr[i][key] = arr[i][key]
    }
  }
  return [].concat(arr)
}
const transArrToLabel1ByName = (arr = []) => {
  if (arr.length == 0) {
    return
  }
  for (let i = 0; i < arr.length; i++) {
    for (let key in arr[i]) {
      arr[i]["label"] = arr[i]["name"]
      arr[i][key] = arr[i][key]
    }
  }
  return [].concat(arr)
}
const transArrById1 = (id, obj) => {
  let arr = obj[id]
  if (JSON.stringify(obj) == "{}") {
    return
  }
  return transArrToLabel1(arr)
}
format.formatTree1 = (arr, obj) => {
  let newArr = transArrToLabel1ByName(arr)
  for (let i = 0; i < newArr.length; i++) {
    newArr[i].children = transArrById1(newArr[i].id, obj)
  }
  return newArr
}

// format.spliceArrByCreated = (optionArr, moment) => {   // 数组中去掉重复元素，保留不重复元素
// for (var i = optionArr.length - 1; i >= 0; i--) {     var targetNode =
// optionArr[i];     for (var j = 0; j < i; j++) {       if
// (moment(targetNode.createdAt).format('HH') ==
// moment(optionArr[j].createdAt).format('HH')) {         optionArr.splice(i,
// 1);         break;       }     }   }   return optionArr }
export default format
